package cn.edu.neu.action;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.neu.model.User;
import cn.edu.neu.model.Systemset;
import cn.edu.neu.service.SystemsetService;
import cn.edu.neu.service.UserService;

@Controller
public class UserAction {

	@Autowired
	private UserService userService;
	@Autowired
	private SystemsetService sysService;


	@RequestMapping("/login")
	public String loginView() {
		return "login";
	}

	@SuppressWarnings("unused")
	@RequestMapping("/UserCenter.do")
	public String login(String username, String password, Model model,HttpSession session) {
		System.out.println("username:" + username);
		System.out.println("password:" + password);
		Map<String, Object> userMap = userService.findByName(username);
		if (userMap == null) {
			model.addAttribute("info", "用户名不能为空");
			return "login";
		}
		else {
			String oldPass = (String)userMap.get("password");
			String log_times = (String)userMap.get("login_times");
			String id = (String)userMap.get("id");
			String status = (String)userMap.get("status");
			if(status.equals("冻结")){
				model.addAttribute("info", "账号已被冻结");
				return "login";
			}
			if(oldPass.equals(password)){
				if(log_times.equals("3")){
					model.addAttribute("infopass", "请修改密码！");
					return "/UserCenter/ModifyPass";
				}else{
				   userService.inlogtimes(id);
				   session.setAttribute("LOGINUSER", userMap);
				   return "/UserCenter/index";
				}
			}else{
				model.addAttribute("info", "密码错误");
				return "login";
			}
		}

		/*return "login";*/
	}
	
	@RequestMapping("/home")
	public String home() {
		return "/UserCenter/home";
	}
	
	
	@RequestMapping("/UserMessages")
	public String UserMessages() {
		return "/UserCenter/UserMessages";
	}
	
	@RequestMapping("/AddUserT")
	public String AddUserT(Model model) {
		List<User> laf=userService.getLeadAndReF();
		List<Systemset> sysmgname=sysService.getMemGname();
		model.addAttribute("laf",laf);
		model.addAttribute("sysmgname",sysmgname);
		
		return "/UserCenter/AddMember";
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/AddMember")
	public String AddMember(User user,Systemset sysset,Model model,HttpSession session) {
		
		Map<String, Object> userMap=(Map<String, Object>) session.getAttribute("LOGINUSER");
		
		String id=(String) userMap.get("id");
		
		String lc= userService.getLeadCount(id);
		
		
		String grade=(String) userMap.get("grade");
		
		String mg_id=sysset.getMg_id();
		
		String xiadai=sysService.getxiadai(grade,mg_id);
		
		String unit=sysset.getUnit();
		String rr_reward=sysset.getReward();
		/*System.out.println(rr_reward);*/
		
		int result = xiadai.compareTo(lc);
		
		String mgn=(String) userMap.get("member_grade_name");
		String rr_rule=(String) userMap.get("rule");
		/*获取更新时间*/
		Date now = new Date(); 
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String update_time = dateFormat.format(now); 
		
		String uuid = UUID.randomUUID().toString();
		user.setId(uuid);
		user.setMa_id(uuid);
		user.setRr_reward(rr_reward);
		user.setRr_rule(rr_rule);
		
		user.setRr_grade(grade);
		user.setMg_id(mg_id);
		
		user.setUpdate_time(update_time);
		user.setReward_unit(unit);
		
			if(mgn.equals("超级管理员")){
				
				if(result>0)
				{
					if(unit.equals("地币")){
						  
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjdb(user);
							userService.memrecordtj(user);
							return "redirect:FindAllUser.do";
					}
					if(unit.equals("金豆")){
						
						
						userService.AddMemBer(user);
						userService.AddMhMg(user);
						userService.AddMhr(user);
						userService.AddMA(user);
						user.setTj_id(id);
						userService.updatetjjd(user);
						userService.memrecordtj(user);
						return "redirect:FindAllUser.do";
					}
					  
					
				}
				else{
					model.addAttribute("infoadd", "添加失败！下级已满");
					return "redirect:AddUserT.do";
					
					   
				}
				
			}else{
				/*一代*/
				if(grade.equals("1"))
				{
					if(result>1)
					{
						if(unit.equals("地币")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjdb(user);
							userService.memrecordtj(user);
							return "redirect:FindAllUser.do";
						}
						if(unit.equals("金豆")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjjd(user);
							userService.memrecordtj(user);
							return "redirect:FindAllUser.do";
						}
					}
					
					else if(result==1)
					{
						if(unit.equals("地币")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjdb(user);
							userService.AddRuleOT(user);
							userService.memrecordtj(user);
							userService.memrecordjl(user);
							return "redirect:FindAllUser.do";
						}
						if(unit.equals("金豆")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							
							user.setTj_id(id);
							userService.updatetjjd(user);
							userService.AddRuleOT(user);
							userService.memrecordtj(user);
							userService.memrecordjl(user);
							return "redirect:FindAllUser.do";
						}
					}
					else
					{
						model.addAttribute("infoadd", "添加失败！下级已满");
						return "/UserCenter/AddMember";
					}
					
				}
				/*二代*/
				else if(grade.equals("2"))
				{
					if(result>1)
					{
						if(unit.equals("地币")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							user.setTj_id(id);
							userService.updatetjdb(user);
							userService.AddRuleOT(user);
							userService.memrecordtj(user);
							return "redirect:FindAllUser.do";
						}
						if(unit.equals("金豆")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjjd(user);
							userService.AddRuleOT(user);
							userService.memrecordtj(user);
							return "redirect:FindAllUser.do";
						}
					}
					
					else if(result==1)
					{
						if(unit.equals("地币")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjdb(user);
							userService.memrecordtj(user);
							userService.memrecordjl(user);
							return "redirect:FindAllUser.do";
						}
						if(unit.equals("金豆")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjjd(user);
							userService.memrecordtj(user);
							userService.memrecordjl(user);
							return "redirect:FindAllUser.do";
						}
					}
					else
					{
						model.addAttribute("infoadd", "添加失败！下级已满");
						return "/UserCenter/AddMember";
					}
				}
				/*三代之后*/
				else
				{
					if(result>1)
					{
						if(unit.equals("地币")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							user.setTj_id(id);
							userService.updatetjdb(user);
							userService.AddRuleThreeAfter(user);
							userService.memrecordtj(user);
							return "redirect:FindAllUser.do";
						}
						if(unit.equals("金豆")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjjd(user);
							userService.AddRuleThreeAfter(user);
							userService.memrecordtj(user);
							return "redirect:FindAllUser.do";
						}
					}
					
					else if(result==1)
					{
						if(unit.equals("地币")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjdb(user);
							userService.memrecordtj(user);
							userService.memrecordjlta(user);
							return "redirect:FindAllUser.do";
						}
						if(sysset.getUnit().equals("金豆")){
							
							userService.AddMemBer(user);
							userService.AddMhMg(user);
							userService.AddMhr(user);
							userService.AddMA(user);
							user.setTj_id(id);
							userService.updatetjjd(user);
							userService.memrecordtj(user);
							userService.memrecordjlta(user);
							return "redirect:FindAllUser.do";
						}
					}
					else
					{
						model.addAttribute("infoadd", "添加失败！下级已满");
						return "/UserCenter/AddMember";
					}
					
				}
			}
			return "login";
			
		
		
	}
	
	

	@RequestMapping("/FindAllUser")
	public String FindAllUser(Model model) {
		List<User> users=userService.getAllUser();
		/*System.out.println("usersSize"+users.size());*/
		model.addAttribute("users", users);
		return "/UserCenter/FindAllUser";
	}
	
	@RequestMapping("/subordinate")
	public String Subordinate(String id,Model model) {
		/*System.out.printf(id);*/
		List<User> users=userService.getSbdUser(id);
		/*System.out.println("usersSize"+users.size());*/
		model.addAttribute("users", users);
		return "/UserCenter/subordinate";
	}
	
	@RequestMapping("/memberrecord")
	public String memberrecord(Model model,HttpSession session) {
        Map<String, Object> userMap=(Map<String, Object>) session.getAttribute("LOGINUSER");
		String id=(String) userMap.get("id");
		List<User> memjl=userService.getmemjl(id);
		model.addAttribute("memjl", memjl);
		return "/UserCenter/memberrecord";
	}
	
	@RequestMapping("/ModifyPass")
	public String ModifyPass() {	
		return "/UserCenter/ModifyPass";
	}
	
	 /*验证身份证号和真实姓名 修改密码*/
	@RequestMapping("/VerifyIdentity")
	public String VerifyIdentity(String realname, String idcard, Model model,HttpSession session) {
		System.out.println("idcard:" + idcard);
		System.out.println("realname:" + realname);
		Map<String, Object> VIMap = userService.findVerifyIdentity(idcard,realname);
		if(VIMap==null){
			model.addAttribute("infopass","身份证号或真实姓名错误！");
			return "/UserCenter/ModifyPass";
		}
		else{
		session.setAttribute("VIMap", VIMap);
		return "/UserCenter/ModifyPass2";
		}
		/*return "/UserCenter/ModifyPass";*/
	}
	
	@RequestMapping("/xiupass")
	public String xiupass(String password,Model model,HttpSession session) {	
		Map<String, Object> VIMap=(Map<String, Object>) session.getAttribute("VIMap");
		String id=(String) VIMap.get("id");
		userService.xiupass(id,password);
		return "redirect:login.do";
	}
	
	@RequestMapping("/LoginOut")
	public String LoginOut() {	
		return "login";
	}

	

}
